//
//  RHLocalDeviceVC.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/3/26.
//

#import "RHLocalDeviceVC.h"
#import "JXCategoryView.h"
#import "JXCategoryListContainerView.h"
#import "RHLocalDevSystemVC.h"
#import "RHNavigationController.h"
#import "RHLocalStatisticsVC.h"
#import "CBLECenterMnger.h"
#import "RHDeviceInfoListVC.h"
#import "RHLocalAlarmFaultVC.h"
#import "RHNavgationView.h"

@interface RHLocalDeviceVC ()<JXCategoryListContainerViewDelegate,JXCategoryViewDelegate,JXCategoryTitleViewDataSource>

@property (nonatomic, strong) NSArray *titles;

@property (nonatomic, strong) JXCategoryTitleView *categoryView;

@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;

@property (nonatomic, strong) UIView *gradentView;

@property (nonatomic, assign) NSInteger index;

@property (nonatomic, strong) RHNavgationView *navView;
@end

@implementation RHLocalDeviceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = CFTabViewBgColor;
   
    [self setupUI];
    kWeakSelf;
    RHNavigationController *nav = (RHNavigationController*)self.navigationController;
    nav.backActionBlock = ^{
        [[RHBleCmdManager shareMnger]removeAllCommand];
        [ProgressHUDManage hideTips];
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
//    [(RHNavigationController*)self.navigationController setBackgoundClear:self.index == 0?YES:NO titleColor:nil];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    [(RHNavigationController*)self.navigationController setBackgoundClear:NO titleColor:nil];
}



- (void)setupUI{
    
    UIView *topView = kView;
    [self.view addSubview:topView];
    self.gradentView = topView;
    [topView setGradientBackgroundWithColors:@[kRGB(188, 222, 251),
                                               kRGB(230, 240, 249),
                                               kRGB(239, 240, 241),
                                               kRGB(239, 238, 241),
                                               CFTabViewBgColor] locations:@[@(0),@(0.7),@(0.8),@(0.9),@(1)] startPoint:CGPointMake(0.5, 0) endPoint:CGPointMake(0.5, 1)];
    
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.top.right.mas_offset(0);
        make.height.offset(kUI_WINDOW_W*180.0/232+kNavHeight+50);
    }];
    //自定义导航栏
    _navView = [[RHNavgationView alloc]initWithController:self];
    _navView.title = [CBLECenterMnger shareMnger].curPheral.devName;
    [self.view addSubview:_navView];
    
    self.titles = @[@"系统".rh_localized,@"统计".rh_localized];
    
    UIButton *setButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,30, 64)];
    [setButton setImage:[UIImage imageNamed:@"device_ble_detail"] forState:UIControlStateNormal];
    [setButton setBackgroundColor:UIColor.clearColor];
    [setButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [setButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
//        [setButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [setButton addTarget:self action:@selector(deviceInfo) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *detailBarBtn = [[UIBarButtonItem alloc] initWithCustomView:setButton];
    
    UIButton *alarmBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,30, 64)];
    [alarmBtn setImage:[UIImage imageNamed:@"device_alarm_02"] forState:UIControlStateNormal];
    [alarmBtn setBackgroundColor:UIColor.clearColor];
    [alarmBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [alarmBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
//        [alarmBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [alarmBtn addTarget:self action:@selector(alarmInfo) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *alarmBarBtn = [[UIBarButtonItem alloc] initWithCustomView:alarmBtn];
//    self.navigationItem.rightBarButtonItems = @[alarmBarBtn];
 
    
    
    _listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    [self.view addSubview:self.listContainerView];
    
    _categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, 50)];

    self.categoryView.titles = self.titles;
    self.categoryView.backgroundColor = [UIColor clearColor];
    self.categoryView.delegate = self;
    self.categoryView.cellWidth = JXCategoryViewAutomaticDimension;
    self.categoryView.titleSelectedColor = CMainColorGreen;
    self.categoryView.titleColor = kCOLOR_HEX(@"969799");
    self.categoryView.titleFont = [UIFont systemFontOfSize:14];
    self.categoryView.titleSelectedFont = [UIFont boldSystemFontOfSize:14];
    self.categoryView.titleColorGradientEnabled = YES;
    self.categoryView.titleLabelZoomEnabled = NO;
    self.categoryView.titleLabelZoomScale = 1.0;
    self.categoryView.cellSpacing = 20;
    self.categoryView.contentEdgeInsetLeft = 60;
    self.categoryView.contentEdgeInsetRight = 60;
    self.categoryView.titleLabelVerticalOffset = -10;
    [self.view addSubview:self.categoryView];
    self.categoryView.listContainer = self.listContainerView;
    
    JXCategoryIndicatorBackgroundView *backgroundView = [[JXCategoryIndicatorBackgroundView alloc] init];
//     backgroundView.size = CGSizeMake(60, 3);
     backgroundView.indicatorColor = CMainColorGreen;
     backgroundView.indicatorHeight = 3;
//     backgroundView.indicatorWidth = 60;
     backgroundView.verticalMargin = -10;
     backgroundView.indicatorCornerRadius = 1.5;

     _categoryView.indicators = @[backgroundView];
    
    
    [_categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_offset(kNavHeight);
        make.height.mas_equalTo(50);
    }];
    
    [_listContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(self.categoryView.mas_bottom).offset(0);
        make.bottom.mas_offset(0);
    }];
    
    
}


-(void)deviceInfo
{
    RHDeviceInfoListVC *detailVC = [[RHDeviceInfoListVC alloc]init];
    detailVC.isFromBle = YES;
    [self.navigationController pushViewController:detailVC animated:YES];
}


-(void)alarmInfo
{
    RHLocalAlarmFaultVC *alarmVC = [[RHLocalAlarmFaultVC alloc]init];
    [self.navigationController pushViewController:alarmVC animated:YES];
}

#pragma mark - JXCategoryViewDelegate

// 点击选中或者滚动选中都会调用该方法。适用于只关心选中事件，不关心具体是点击还是滚动选中的。
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    NSLog(@"%@", NSStringFromSelector(_cmd));
    // 侧滑手势处理
    self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
    
//    [(RHNavigationController*)self.navigationController setBackgoundClear:index == 0 titleColor:nil];
//    [_categoryView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_offset(index == 0?kNavHeight:0);
//    }];
    _navView.backgroundColor = index == 0?UIColor.clearColor:UIColor.whiteColor;
    _categoryView.backgroundColor = index == 0?UIColor.clearColor:UIColor.whiteColor;
    self.gradentView.hidden = index != 0;
    self.index = index;

}

// 滚动选中的情况才会调用该方法
- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

#pragma mark - JXCategoryListContainerViewDelegate
// 返回列表的数量
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return  self.titles.count;
}

// 返回各个列表菜单下的实例，该实例需要遵守并实现 <JXCategoryListContentViewDelegate> 协议
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    if(index == 0){
        RHLocalDevSystemVC *systemVC = [[RHLocalDevSystemVC alloc]init];
        
        return systemVC;
    }else{
        RHLocalStatisticsVC *statisticsVC = [[RHLocalStatisticsVC alloc]init];
        return  statisticsVC;
    }
    
    
  
}

-(void)dealloc
{
    [[RHBleCmdManager shareMnger]cancelTimeOutAction];
//    [[CBLECenterMnger shareMnger] cmdDisconnectCurPheral];
}

@end
